//
//  ViewController.swift
//  OriginLayoutDemo
//
//  Created by lantaiyuan on 2017/7/11.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    // 用户
    lazy var userTF:UITextField = {
        let tf = UITextField()
        tf.placeholder = "UserName"
        tf.borderStyle = .roundedRect
        return tf
    }()
    
    // 密码
    lazy var passwordTF:UITextField = {
        let tf = UITextField()
        tf.placeholder = "Password"
        tf.borderStyle = .roundedRect
        return tf
    }()
    
    // 登录
    lazy var singInBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(.red, for: .normal)
        btn.backgroundColor = .yellow
        return btn
    }()
    
    // 注册
    lazy var singUpBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(.red, for: .normal)
        btn.backgroundColor = .yellow
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }

}


extension ViewController {
    func setupUI() {
        // 1.添加控件
        view.addSubview(userTF)
        view.addSubview(passwordTF)
        view.addSubview(singInBtn)
        view.addSubview(singUpBtn)
        
        // 2.取消 autoresizing
        for v in view.subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 3.自动布局
        // layout2()  // 自动布局
        layout2()   // VFL
        
    }
    
    //format ：VFL语句
    //opts ：约束类型
    //metrics ：VFL语句中用到的具体数值
    //views ：VFL语句中用到的控件
    func layout2() {
        let viewDict = ["userTF": userTF, "passwordTF": passwordTF, "singInBtn": singInBtn, "singUpBtn": singUpBtn] as [String : Any]
        
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-20-[userTF]-20-|",
            options: [],
            metrics: nil,
            views: viewDict))
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-20-[passwordTF(==userTF)]",
            options: [],
            metrics: nil,
            views: viewDict))
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-64-[userTF]-20-[passwordTF(==passwordTF)]-20-[singInBtn]",
            options: [],
            metrics: nil,
            views: viewDict))

        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-20-[singInBtn]-20-[singUpBtn(==singInBtn)]-20-|",
            options: [],
            metrics: nil,
            views: viewDict))
        
        view.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:[passwordTF]-20-[singUpBtn]",
            options: [],
            metrics: nil,
            views: viewDict))
    }
    
    
    // NSLayoutConstraint
    func layout1() {
        // username
        view.addConstraint(NSLayoutConstraint(item: userTF,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .top,
                                              multiplier: 1.0,
                                              constant: 64))
        
        view.addConstraint(NSLayoutConstraint(item: userTF,
                                              attribute: .right,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: -20))
        
        view.addConstraint(NSLayoutConstraint(item: userTF,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .left,
                                              multiplier: 1.0,
                                              constant: 20))
        // password
        view.addConstraint(NSLayoutConstraint(item: passwordTF,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: userTF,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 20))
        
        view.addConstraint(NSLayoutConstraint(item: passwordTF,
                                              attribute: .right,
                                              relatedBy: .equal,
                                              toItem: userTF,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: passwordTF,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: userTF,
                                              attribute: .left,
                                              multiplier: 1.0,
                                              constant: 0))
        
        // singin
        view.addConstraint(NSLayoutConstraint(item: singInBtn,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: passwordTF,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 20))
        
        view.addConstraint(NSLayoutConstraint(item: singInBtn,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: passwordTF,
                                              attribute: .left,
                                              multiplier: 1.0,
                                              constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: singInBtn,
                                              attribute: .right,
                                              relatedBy: .equal,
                                              toItem: passwordTF,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: -10))
        
        // singup
        view.addConstraint(NSLayoutConstraint(item: singUpBtn,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: singInBtn,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: 20))
        
        view.addConstraint(NSLayoutConstraint(item: singUpBtn,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: singInBtn,
                                              attribute: .top,
                                              multiplier: 1.0,
                                              constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: singUpBtn,
                                              attribute: .right,
                                              relatedBy: .equal,
                                              toItem: passwordTF,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: 0))
        
        
        
    }
}
